package cn.itcast.mq.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class CommonConfig {

    public static final String DIRECT_EXCHANGE = "direct.exchange";
    public static final String DIRECT_ROUTE_KEY = "direct.key";
    public static final String DIRECT_QUEUE = "direct.queue";


    /**
     * 交换机的三种持久化方式
     *
     * @return
     */
    @Bean
    public DirectExchange directExchange() {
//        return new DirectExchange(DIRECT_EXCHANGE); //第一种
        //return new DirectExchange(DIRECT_EXCHANGE,true,false); //第二种
        return ExchangeBuilder.directExchange(DIRECT_EXCHANGE).durable(true).build();
    }


    /**
     * 队列的三种持久化方式
     *
     * @return
     */
    @Bean
    public Queue directQueue() {
//        return new Queue(DIRECT_QUEUE); //第一种
//        return new Queue(DIRECT_QUEUE,true);
        return QueueBuilder.durable(DIRECT_QUEUE).build();
    }

    @Bean
    public Binding bindDirectQueue() {
        return BindingBuilder.bind(directQueue()).to(directExchange()).with(DIRECT_ROUTE_KEY);
    }
}
